<?php

$obj = new scws(date('Ymd'));
$obj->scws();

class scws {

    public function scws() {
        $this->segmentation_note();
        $this->segmentation_circle();
    }

    public function segmentation_note()
    {
        $objNote = new \app\api\model\dao\Note();
        $objWordLibrary = new \app\api\model\dao\WordLibrary();

        $date = date('Ymd');

        $cond = [
            'date' => ['=', $date],
            'is_delete' => ['=', \app\library\Flag::CONST_NOTE_DELETE_NOT],
        ];
        do {
            $ret = $objNote->getNote($cond, ['id', 'title', 'content']);
            if (empty($ret)) {
                break;
            }
            $word_note = [];
            foreach ($ret as $val) {
                $words = \app\library\Common::scws($val['title']);
                if (!empty($words)) {
                    foreach ($words as $word) {
                        $word_note[] = [
                            'word' => $word,
                            'type' => \app\library\Flag::CONST_WORD_TYPE_NOTE,
                            'obj_id' => $val['id'],
                        ];
                    }
                }
            }
            if (!empty($word_note)) {
                $ret = $objWordLibrary->addWordBatch($word_note);
                if (false === $ret) {
                    \think\Log::error(__METHOD__ . ' scws fail.');
                }
            }
        } while (1);

    }

    public function segmentation_circle()
    {
        $objCircle = new \app\api\model\dao\Circle();
        $objWordLibrary = new \app\api\model\dao\WordLibrary();
        $cond = [
            'approve_status' => ['=', \app\library\Flag::CONST_CIRCLE_APPROVE_PASS],
            'is_delete' => ['=', \app\library\Flag::CONST_CIRCLE_DELETE_NOT],
            'create_time' => ['>', time() - 86400],
        ];
        do {
            $ret = $objCircle->getCircle($cond, ['id', 'name']);
            if (empty($ret)) {
                break;
            }
            $word_circle = [];
            foreach ($ret as $val) {
                $words = \app\library\Common::scws($val['name']);
                if (!empty($words)) {
                    foreach ($words as $word) {
                        $word_circle[] = [
                            'word' => $word,
                            'type' => \app\library\Flag::CONST_WORD_TYPE_CIRCLE,
                            'obj_id' => $val['id'],
                        ];
                    }
                }
            }
            if (!empty($word_circle)) {
                $ret = $objWordLibrary->addWordBatch($word_circle);
                if (false === $ret) {
                    \think\Log::error(__METHOD__ . ' scws fail.');
                }
            }
        } while (1);
    }
}

